package com.czy.owner.ui.information;

import android.content.Intent;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.czy.owner.R;
import com.czy.owner.adapter.StrategyListAdapter;
import com.czy.owner.api.StrategyListApi;
import com.czy.owner.callback.RecycleViewDivider;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.FindStrategyModel;
import com.czy.owner.entity.StrategyListModel;
import com.czy.owner.global.Constants;
import com.czy.owner.net.api.BaseResultEntity;
import com.czy.owner.net.http.HttpManager;
import com.czy.owner.net.listener.HttpOnNextListener;
import com.czy.owner.rxbus.RxBus;
import com.czy.owner.rxbus.Subscribe;
import com.czy.owner.rxbus.ThreadMode;
import com.czy.owner.rxbus.event.CommonEvent;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.utils.PhoneUtils;
import com.easyrecycleview.EasyRecyclerView;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;

import butterknife.BindView;

/**
 * Created by wangyang on 2017/5/16 0016.
 * 攻略列表
 */

public class StrategyActivity extends BaseActivity implements RecyclerArrayAdapter.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.easy_recycleview)
    EasyRecyclerView easyRecyclerView;
    private StrategyListAdapter adapter;
    private Handler handler = new Handler();
    private int page = 1;
    private FindStrategyModel model = new FindStrategyModel();

    @Override
    protected int attachLayoutRes() {
        return R.layout.common_activity_easyrecycleview;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
//        RxBus.getDefault().register(this);
        model = (FindStrategyModel) getIntent().getSerializableExtra("model");
        getToolbarTitle().setText(model.getInfoBaseName());
        easyRecyclerView.setLayoutManager(new LinearLayoutManager(StrategyActivity.this));
        easyRecyclerView.addItemDecoration(new RecycleViewDivider(StrategyActivity.this, LinearLayoutManager.VERTICAL, R.drawable.shape_recyclerview_divider_decoration));
        easyRecyclerView.setAdapterWithProgress(adapter = new StrategyListAdapter(StrategyActivity.this, model.getInfoBaseName()));
        adapter.setMore(R.layout.view_more, this);
        adapter.setNoMore(R.layout.view_nomore, new RecyclerArrayAdapter.OnNoMoreListener() {
            @Override
            public void onNoMoreShow() {
                //adapter.resumeMore();
            }

            @Override
            public void onNoMoreClick() {
                adapter.resumeMore();
            }
        });

        adapter.setError(R.layout.view_error_net, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();

            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();

            }
        });

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener<StrategyListModel>() {
            @Override
            public void onItemClick(View view, int position, StrategyListModel model) {
                Intent ti = new Intent(StrategyActivity.this, InformationDetailActivity.class);
                ti.putExtra("title", model.getTitle());
                ti.putExtra("content", model.getContent());
                ti.putExtra("imgUrl", model.getCoverUrl());
                ti.putExtra("infoId", model.getInfoId());
                ti.putExtra("isCollect", model.isCollect());
                ti.putExtra("collectType", "strategy");
                //ti.putExtra("strategyListModel",(Serializable)model);
                startActivity(ti);

            }
        });
        easyRecyclerView.setRefreshListener(this);
        onRefresh();
    }

    @Override
    protected void updateViews() {

    }


    private void getDataList() {
        StrategyListApi strategyListApi = new StrategyListApi(new HttpOnNextListener<List<StrategyListModel>>() {
            @Override
            public void onNext(List<StrategyListModel> list) {
                setDataList(list);
            }

            @Override
            public void onCacheNext(String cache) {
                super.onCacheNext(cache);
                Gson gson = new Gson();
                Type type = new TypeToken<BaseResultEntity<List<StrategyListModel>>>() {
                }.getType();
                BaseResultEntity<List<StrategyListModel>> cacheList = gson.fromJson(cache, type);
                setDataList(cacheList.getData());
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                PhoneUtils.ShowToastMessage(StrategyActivity.this, e.getMessage());
            }
        }, this, page + "");
        strategyListApi.setSession(UserHelper.getInstance().getSession(StrategyActivity.this));
        strategyListApi.setPage(page + "");
        strategyListApi.setSize("10");
        strategyListApi.setInfoBaseId(model.getInfoBaseId() + "");
        HttpManager manager = HttpManager.getInstance();
        manager.doHttpDeal(strategyListApi);
    }

    private void setDataList(List<StrategyListModel> list) {
        if (list.size() < 9) {
            adapter.stopMore();
        }
        adapter.addAll(list);
        if (adapter.getAllData().size() == 0) {
            easyRecyclerView.setEmptyView(R.layout.view_empty_common);
            easyRecyclerView.showEmpty();
        }
    }


    @Override
    public void onRefresh() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //刷新
                adapter.clear();
                page = 1;
                getDataList();
            }
        }, 1000);
    }

    @Override
    public void onLoadMore() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //刷新
                if (!PhoneUtils.isNetworkConnected(StrategyActivity.this)) {
                    adapter.pauseMore();
                    easyRecyclerView.showError();
                    return;
                }
                page++;
                getDataList();

            }
        }, 1000);
    }

//    @Subscribe(code = Constants.COLLECTION, threadMode = ThreadMode.MAIN)
//    public void collection(CommonEvent event) {
//        onRefresh();
//    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        RxBus.getDefault().unRegister(this);
    }
}
